package com.navinfo.collect.library.data.flutter.datahandler

import android.content.Context
import android.util.Log
import com.navinfo.collect.library.data.RealmUtils
import com.navinfo.collect.library.data.dao.impl.MapLifeDataBase
import com.navinfo.collect.library.data.datahandler.DataElementHandler
import com.navinfo.collect.library.data.entity.Element
import com.navinfo.collect.library.data.DataConversion
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import org.oscim.core.GeoPoint
import kotlin.concurrent.thread

/**
 * 数据操作
 */
class FlutterDataElementHandler(context: Context, dataBase: MapLifeDataBase) :
    DataElementHandler(context, dataBase) {

    /**
     * 保存数据
     */
    fun saveElementData(call: MethodCall, result: MethodChannel.Result) {
        try {
            if (call.arguments is Map<*, *>) {
                val element = Element();
                element.layerId = call.argument("layerId")
                element.id = call.argument("uuid")
                element.geometry = call.argument("geometry")
                element.displayText = call.argument("displayText")
                element.operationTime = call.argument("tOperateDate")
                element.tLifecycle = call.argument<Int>("tLifecycle")!!
                element.tStatus = call.argument<Int>("tStatus")!!
                val valueMap = call.argument<Map<String, Any>>("values")
                saveData(element, valueMap) { res, error ->
                    if (res) {
                        result.success("$res")
                    } else {
                        result.success(error);
                    }
                };

            } else {
                result.success("数据格式错误")
            }
        } catch (e: Throwable) {
            Log.e("jingo", e.message);
            Log.e("jingo", e.stackTraceToString());
            Log.e("jingo", e.toString());
            result.success("数据格式错误")
        }
    }

    fun importPbfData2Realm(call: MethodCall, result: MethodChannel.Result) {
        if (call.arguments is List<*>) {
            val pbfFiles: List<String> = call.arguments as List<String>;
            try {
                val importResult = RealmUtils.getInstance().importPbfData(pbfFiles);
                result.success(importResult)
            } catch (exeception: Exception) {
                result.error("-1", exeception.message, exeception);
            }
        }
    }

    /**
     * 捕捉数据
     */
    fun snapElementDataList(call: MethodCall, result: MethodChannel.Result) {
        if (call.arguments is String) {
            snapElementDataList(call.arguments as String) {
                result.success(DataConversion.toElementMapList(it))
            }
        }
    }

    fun queryElementDeepInfo(call: MethodCall, result: MethodChannel.Result) {
        if (call.arguments is Map<*, *>) {
            val id = call.argument<String>("id");
            val layerId = call.argument<String>("layerId");
            if (id != null && layerId != null) {
                queryElementDeepInfo(id, layerId) { layerManager, itemList ->
                    if (layerManager != null) {
                        val map = layerManager.toMap()
                        map["layerItems"] = DataConversion.customLayerItemsToMapList(itemList)
                        result.success(map)
                    } else {
                        result.success("没有这条数据的拓展模板")
                    }
                }
            }
        }
    }
}